﻿using System;
using System.Xml.Linq;
using Microsoft.SharePoint;
using SPQueryToSql.Extensions;

namespace SPQueryToSql.ApplicationObjects
{
    /// <summary>
    /// Represents SharePoint Field
    /// </summary>
    public class FieldInfo
    {
        ///<summary>
        /// Create a new instance of FieldInfo
        ///</summary>
        ///<param name="field">List' Field</param>
        public FieldInfo(SPField field)
        {
            Id = field.Id;
            Title = field.Title;
            WebId = field.ParentList.ParentWeb.ID;
            SiteId = field.ParentList.ParentWeb.Site.ID;
            
            InternalName = field.InternalName;
            Type = field.Type;
            
            var element = XElement.Parse(field.SchemaXml);
            RowOrdinal = element.AttributeValueInt("RowOrdinal", 0);
            ColumnName = element.AttributeValueString("ColName", string.Empty);
            IsMulti = element.AttributeValueBool("Mult", false);
            if (field.Type == SPFieldType.Lookup && !string.IsNullOrEmpty(ColumnName))
            {
                var web = field.ParentList.ParentWeb;
                var listStr = element.AttributeValueString("List", string.Empty);
                var lookupListId = new Guid(listStr);
                var lookupList = web.Lists.GetList(lookupListId, false);
                LookupFieldInternalName = element.AttributeValueString("ShowField", string.Empty);
                var lookupField = lookupList.Fields.GetFieldByInternalName(LookupFieldInternalName);
                LookupFieldColumn = XElement.Parse(lookupField.SchemaXml).AttributeValueString("ColName", string.Empty);
                LookupListId = lookupListId;
            }

            if(Type == SPFieldType.ContentTypeId)
            {
                ColumnName = "tp_ContentTypeId";
                InternalName = "ContentTypeId";
            }
        }

        /// <summary>
        /// Identifier
        /// </summary>
        public Guid Id;
        /// <summary>
        /// Display name
        /// </summary>
        public string Title;
        /// <summary>
        /// InternalName
        /// </summary>
        public string InternalName;
        /// <summary>
        /// Column' name in the dbo.AllUserData table
        /// </summary>
        public string ColumnName;
        /// <summary>
        /// Lookup field' sql column
        /// </summary>
        public string LookupFieldColumn;
        /// <summary>
        /// Lookup field' internal name
        /// </summary>
        public string LookupFieldInternalName;
        /// <summary>
        /// Lookup list' Id
        /// </summary>
        public Guid LookupListId;
        /// <summary>
        /// SPWeb Id
        /// </summary>
        public Guid WebId;
        /// <summary>
        /// SPSite Id
        /// </summary>
        public Guid SiteId;
        /// <summary>
        /// Multivalue 
        /// </summary>
        public bool IsMulti;
        /// <summary>
        /// Field type
        /// </summary>
        public SPFieldType Type;
        /// <summary>
        /// RowOrdinal in the dbo.AllUserData table
        /// </summary>
        public int RowOrdinal;
    }
}
